var app = new Vue({
	el: '#app',
	data: {
		uploadurl: "",
		percent:0,
		t:0
	},
	mounted(e) {
		setInterval(()=>{
			this.t = this.t + 1
		},2000)
		const _this = this
		this.percent = 0
		window.addEventListener('message', e => {
			console.log(e, 'HTML接受APP');
			_this.receviceMessage(JSON.parse(e.data))
			// app.receviceMessage(e.data.data)
		}, false)
		window.addEventListener("beforeunload", function() {

			uni.postMessage({
				data: {
					type: "exitmtroom",
					payload: {

					}
				}
			});
		})
	},
	onLoad(e) {
		console.log(11, e)
	},
	onShow(e) {
		console.log(22, e)
	},
	methods: {
		receviceMessage: function(e) {
			console.log(e.type, e.payload)
		},

		exit: function() {
			uni.postMessage({
				data: {
					type: "exitmtroom",
					payload: {

					}
				}
			});
			uni.switchTab({
				url: "/pages/meeting/index"
			})
		},
		uploadfile:async function(file){
			const _this = this
			return new Promise((r,j)=>{
				// 初始化一个 XMLHttpRequest 实例对象
				const xhr = new XMLHttpRequest()
				// 创建了一个 FormData 实例
				const form = new FormData()
				// 追加文件数据
				form.append('video', file)
				// 初始化一个请求
				const url = _this.uploadurl
				xhr.open('POST', `${url}/upload/upload/video`)
				console.log("rettt1",`${url}/upload/upload/video`)
				// 请求完成
				xhr.onload = function() {
					// 接受服务端传来的数据
					const resp = JSON.parse(xhr.responseText)
					console.log("rettt",resp)
					r(resp)
				}
				// 请求失败
				xhr.onerror = function(e) {
					r(false)
				}
				//设置超时时间 (可选)
				xhr.timeout = 30000;
				xhr.ontimeout = function(event) {
					r(false)
				}
				// 上传进度
				xhr.upload.onprogress = e => {
					const percent = Math.floor((e.loaded / e.total) * 100)
					_this.percent = percent
				}
				// 发送请求
				xhr.send(form)
			})
		},
		filechange: async function(e) {
			const file = e.target.files[0]
			console.log(123456789, file)
			const _this = this
			const ret = await this.uploadfile(file)
			if(ret && ret.success && ret.data){
				uni.postMessage({
					data: {
						type: "upsuccess",
						payload: {
							str:ret.data.pathname,
							name:file.name,
							size:file.size,
						}
					}
				});
			}else{
				uni.postMessage({
					data: {
						type: "uperror",
						payload: {}
					}
				});
			}
			
		},
		init: function(e) {
			console.log(e)
			this.uploadurl = decodeURIComponent(e)
		},
	}
})